home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et-2_2.lha / et2.2 / src / PathLookup.C < prev    next >
C/C++ Source or Header  |  1990-09-19  |  2KB  |  96 lines

  1. //$PathLookup, PathIter$
  2.  
  3. #include "ByteArray.h"
  4. #include "PathLookup.h"
  5. #include "OrdColl.h"
  6. #include "System.h"
  7. #include "String.h"
  8.  
  9. //---- PathLookup ----------------------------------------------------------
  10.  
  11. MetaImpl(PathLookup, (TP(path), TP(paths), 0));
  12.  
  13. PathLookup::PathLookup(char *p)
  14. {
  15.     path= strsave(p);
  16.     paths= 0;
  17.     Scan();
  18. }
  19.  
  20. PathLookup::~PathLookup()
  21. {
  22.     SafeDelete(path);
  23.     if (paths)
  24.     paths->FreeAll();
  25.     SafeDelete(paths);
  26. }
  27.  
  28. bool PathLookup::Lookup(const char *name, char *buf)
  29. {
  30.     // try current working directory
  31.     if (gSystem->AccessPathName((char*)name, 4) == 0) {
  32.     strcpy(buf, name);
  33.     return TRUE;
  34.     }
  35.     if (!paths)
  36.     return FALSE;
  37.     Iter next(paths);
  38.     ByteArray *bp;
  39.     while (bp= (ByteArray*)next()) {
  40.     sprintf(buf, "%s/%s", bp->Str(), name);
  41.     if (gSystem->AccessPathName((char*)buf, 4) == 0)
  42.         return TRUE;
  43.     }
  44.     return FALSE;    
  45. }
  46.  
  47. void PathLookup::Scan()
  48. {
  49.     char *newpath= path;
  50.     
  51.     paths= new OrdCollection(4); 
  52.     if (!newpath)
  53.     return;
  54.     char *p, *q, buf[200];
  55.     for (p= newpath, q= buf;  ; ) {
  56.     if (*p == ':' || *p == '\0') {
  57.         *q= '\0';
  58.         paths->Add(new ByteArray((byte*)buf, -1));
  59.         q= buf;
  60.         if (*p == '\0')
  61.         break;
  62.         p++;
  63.     } else
  64.         *q++= *p++;
  65.     } 
  66. }
  67.  
  68. void PathLookup::Add(char *p)
  69. {
  70.     ByteArray *b= new ByteArray((byte*)p, -1);
  71.     if (!paths->Contains(b))
  72.     paths->Add(b);
  73.     else
  74.     SafeDelete(b);
  75. }
  76.  
  77. //---- class PathIter --------------------------------------------------------
  78.  
  79. PathIter::PathIter(PathLookup *p)
  80. {
  81.     ip= p->paths->MakeIterator();    
  82. }
  83.  
  84. PathIter::~PathIter()
  85. {
  86.     SafeDelete(ip);
  87. }
  88.  
  89. char *PathIter::operator()()
  90. {
  91.     ByteArray *bp= (ByteArray*)ip->operator()();
  92.     if (bp)
  93.     return (char*) bp->Str();
  94.     return 0;
  95. }
  96.